package com.sunwayworld.basemodule.rm.archivesmanage.service.impl;

import com.sunwayworld.basemodule.rm.archivesmanage.bean.LimsArchivesManageBean;
import com.sunwayworld.basemodule.rm.archivesmanage.service.LimsArchivesManageService;
import com.sunwayworld.framework.audit.aunnotation.Audit;
import com.sunwayworld.framework.mybatis.mapper.MatchPattern;
import com.sunwayworld.framework.mybatis.mapper.SearchFilter;
import com.sunwayworld.framework.support.PersistableMetadataHelper;
import com.sunwayworld.module.sys.metadata.service.CoreTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.archivesmanage.dao.LimsArchivesHandleDao;
import com.sunwayworld.basemodule.rm.archivesmanage.bean.LimsArchivesHandleBean;
import com.sunwayworld.basemodule.rm.archivesmanage.service.LimsArchivesHandleService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsArchivesHandleServiceImpl implements LimsArchivesHandleService {

    @Autowired
    private LimsArchivesHandleDao limsArchivesHandleDao;

    @Autowired
    private LimsArchivesManageService limsArchivesManageService;

    @Override
    @SuppressWarnings("unchecked")
    public LimsArchivesHandleDao getDao() {
        return limsArchivesHandleDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsArchivesHandleBean limsArchivesHandle = jsonWrapper.parseUnique(LimsArchivesHandleBean.class);
        limsArchivesHandle.setId(ApplicationContextHelper.getNextIdentity());
        if ("destory".equals(limsArchivesHandle.getHandleType())) {
            LimsArchivesManageBean archivesManageBean = limsArchivesManageService.selectFirstByFilter(SearchFilter.instance().match("archivesCode", limsArchivesHandle.getArchivesCode()).filter(MatchPattern.EQ));
            if (archivesManageBean != null && archivesManageBean.getId() != null) {
                archivesManageBean.setStatus("destoryed");
                limsArchivesManageService.getDao().update(archivesManageBean, "status");
            }
        }
        getDao().insert(limsArchivesHandle);
        return limsArchivesHandle.getId();
    }

    @Transactional
    @AuditTrailEntry(AuditTrailType.DELETE)
    @Audit("AUDIT.DELETE")
    public void delete(RestJsonWrapperBean wrapper) {
        List<LimsArchivesHandleBean> deleteList = wrapper.parse(this.getDao().getType());
        if (!deleteList.isEmpty()) {

            List<Long> ids = deleteList.stream().map(LimsArchivesHandleBean::getId).collect(Collectors.toList());
            LimsArchivesHandleBean destory = this.selectFirstByFilter(SearchFilter.instance().match("handleType", "destory").filter(MatchPattern.EQ).match("id", ids).filter(MatchPattern.OR));
            if (destory != null){
                String archivesCode = destory.getArchivesCode();
                LimsArchivesManageBean archivesManageBean = limsArchivesManageService.selectFirstByFilter(SearchFilter.instance().match("archivesCode", archivesCode).filter(MatchPattern.EQ));
                if (archivesManageBean != null && archivesManageBean.getId() != null) {
                    archivesManageBean.setStatus("draft");
                    limsArchivesManageService.getDao().update(archivesManageBean, "status");
                }
            }

            Class<LimsArchivesHandleBean> itemType = this.getDao().getType();
            CoreTableService tableService = ApplicationContextHelper.getBean(CoreTableService.class);
            deleteList.forEach((t) -> {
                tableService.deleteCascade(PersistableMetadataHelper.getTableName(itemType), (Serializable) t.getId());
            });
        }
    }
}
